<?
include 'db_info.php';
include 'alert.php';
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ko" lang="ko">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>팀 삭제 - 합주시간표</title>
	<style type="text/css">
	body { color: black; font-size: 10pt; font-family: 맑은고딕, 돋움;}
	</style>
</head>
<body>

<?
$teamname = $_GET['teamname'];
$teampw = $_POST['teampw'];
// get row of team
$query = mysqli_query($connect, "SELECT * FROM team WHERE teamname='".$teamname."'");
$team = mysqli_fetch_array($query);
//get row of time
$query = mysqli_query($connect, "SELECT * FROM team_time WHERE teamname='".$teamname."'");
$time = mysqli_fetch_array($query);
mysqli_free_result($query);

// if $teampw matches to database or admin password (==authority), then break out. Or it turns back
switch ($teampw) {
	case $team['teampw']: 
	case 'authority': break;
	default: Alert_back("잘못된 비밀번호입니다");
}

// removal of team row
$query_string = "DELETE FROM team WHERE teamname='".$teamname."'";
$query = mysqli_query($connect, $query_string);
$query_string = "DELETE FROM team_time WHERE teamname='".$teamname."'";
$query = mysqli_query($connect, $query_string);

$week = array("dummy", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday");
// deletion from timetable
for ($i = 1; $i <= 7; $i += 1) { // in 'team_time' table of db, first column is id, second column is team name.
	if (!$team['isTemp']) $whichTable = 'timetable';
	else $whichTable = 'timetable_temp';
	
	if ($time[2*$i]) { // if any value in '2n'th column of '$time' array
		for ($counter = $time[2*$i]; /* start time of the day */ $counter < $time[2*$i +1]; /* start < end */ $counter += 50) {
			$query_string = "UPDATE $whichTable SET `$week[$i]`='' WHERE `time`=$counter";
			$query = mysqli_query($connect, $query_string);
		}
	}
}

Alert_index( "합주팀 삭제 완료" );
?>